<template>
  <t-dialog
    :header="header"
    :visible.sync="visible"
    :onClose="closeDialog"
    :closeOnOverlayClick="false"
    :confirmBtn="disabled?null:{
        content: loading?'提交中...':'确认',
        theme: 'primary',
        loading: loading,
      }"
    @confirm="onSubmit"
    width="60%"
    top="5%"
  >
   <div style="height: 65vh">
     <t-form ref="form"  :colon="true" :data="formData" labelWidth="100px" :rules="rules">
       <t-form-item label="计划名称" name="planName">
         <t-input :readonly="disabled" clearable v-model="formData.planName" :maxLength="100" placeholder="请输入计划名称"></t-input>
       </t-form-item>
       <t-form-item label="所属年度" name="planYear">
         <t-date-picker :disabled="disabled" v-model="formData.planYear" mode="year" clearable/>
       </t-form-item>
       <t-form-item layout="inline">
         <t-form-item  label="申报开始时间" name="startDate">
           <t-date-picker clearable v-model="formData.startDate" placeholder="申报开始时间"/>
         </t-form-item>
         <t-form-item  label="申报截止时间" name="endDate">
           <t-date-picker clearable v-model="formData.endDate" placeholder="申报截止时间"/>
         </t-form-item>
       </t-form-item>
       <t-form-item layout="inline">
         <t-form-item  label="审批开始时间" name="spStartDate">
           <t-date-picker clearable v-model="formData.spStartDate" placeholder="审批开始时间"/>
         </t-form-item>
         <t-form-item  label="审批截止时间" name="spEndDate">
           <t-date-picker clearable v-model="formData.spEndDate" placeholder="审批截止时间"/>
         </t-form-item>
       </t-form-item>
       <t-form-item layout="inline">
         <t-form-item  label="推荐开始时间" name="startDate">
           <t-date-picker clearable v-model="formData.recommendStartDate" placeholder="推荐开始时间"/>
         </t-form-item>
         <t-form-item  label="推荐截止时间" name="endDate">
           <t-date-picker clearable v-model="formData.recommendEndDate" placeholder="推荐截止时间"/>
         </t-form-item>
       </t-form-item>
       <t-form-item label="附件" name="sysAttMainIds">
         <file-upload :disabled="disabled" :multiple="true" :fileList="fileLists" v-model="formData.sysAttMainVo.sysAttMainIds"></file-upload>
       </t-form-item>
       <t-form-item label="说明" name="note">
         <t-textarea
           :readonly="disabled"
           v-model="formData.note"
           placeholder="请输入内容"
           :autosize="{minRows: 5}"
         />
       </t-form-item>
     </t-form>
   </div>
  </t-dialog>
</template>

<script>
import FileUpload from "@/components/FileUpload/index.vue"
import { getFiles } from "@/api/common";
import dayjs from "dayjs";


export default {
  name: "add-edit-plan",
  components:{
    FileUpload
  },
  props: ["header", "objId", 'disabled'],
  data() {
    return {
      visible: true,
      loading: false,
      fileLists: [],
      formData: {
        planName: "",
        planYear: dayjs().year().toString(),
        note: "",
        startDate:'',
        endDate:'',
        recommendStartDate: '',
        recommendEndDate: '',
        sysAttMainVo:{
          sysAttMainIds:[]
        }
      },
      rules: {
        planName: [{ required: true, message: "计划名称不能为空"}],
        planYear: [{ required: true, message: "计划年度不能为空"}],

      }
    }
  },
  watch: {
    objId: {
      immediate: true,
      handler(val) {
        if (val) {
          this.getObj();
        }
      }
    }
  },
  methods: {
    getObj() {
      this.$api.wcx.apply.planGet(this.objId).then((response) => {
        response.data.sysAttMainVo = response.data.sysAttMainVo || {sysAttMainIds:[]}
        this.formData = response.data;
        getFiles(this.objId).then((res) => {
          this.fileLists = []
          const ids = [];
          if (res.data && res.data.length) {
            res.data.forEach((item) => {
              ids.push(item.id);
              this.fileLists.push({ name: item.fileName, url: item.fullFilePath, id: item.id, filePath: item.filePath })
            })
            this.formData.sysAttMainVo.sysAttMainIds = ids;
          }
        });
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    onSubmit() {
      this.$refs.form.validate().then((validateResult) => {
        if (validateResult === true) {
          this.loading = true;
          if (this.objId) {
            this.$api.wcx.apply.planUpdate(this.formData).then((response) => {
              this.loading = false;
              this.$message.success("修改成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          } else {
            this.$api.wcx.apply.planAdd(this.formData).then((response) => {
              this.loading = false;
              this.$message.success("新增成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          }
        }
      });
    },
    closeDialog() {
      this.$emit("update:visible", false);
    },
  }
}
</script>

<style scoped>

</style>
